/**
 * Created by gongxun on 17/1/13.
 */

var PdkTableCardLayer = cc.Layer.extend({
    mBatchNode : null,
    mSelectStatus : null,
    mPlayers : null,
    ctor : function(){
        this._super();

        this.mBatchNode = new cc.Node();
        this.addChild(this.mBatchNode);

        this.mPlayers = [];
        for (var i = 0; i < 3; i++){
            var player = null;
            if (i == 0){
                player = new PdkMyPlayerModel();
            }else{
                player = new PdkPlayerModel();
            }

            var node = new cc.Node();
            this.addChild(node);

            //参数
            player.seat = i;
            player.setPlayCardNode(node);

            //加入
            this.mPlayers.push(player);
        }

        this.mPlayers[0].loadMyInhandCardAction(this)
    },

    addCard : function(){
        var card = new PdkCard();
        this.mBatchNode.addChild(card);
        return card;
    },

    removeAllCards : function(){
        this.mBatchNode.removeAllChildren();
        for (var i = 0; i < 3; i++){
            this.mPlayers[i].reset();
        }
    },

    //发牌动画
    setFapaiAnim : function(cardNumss){
        this.removeAllCards();

        for (var i = 0; i < 1; i++){
            var nums = cardNumss[i];
            mo.pdkCardHelper.sortCard(nums);
            var cards = [];
            for (var j = 0; j < nums.length; j++){
                var card = this.addCard();
                card.setCardNum(nums[j]);
                cards.push(card);
            }

            this.mPlayers[i].setFapai(cards);
        }
    },

    //设置手牌
    setInhandCard : function(cardNumss, isAction){
        for (var i = 0; i < 1; i++){
            var nums = cardNumss[i];
            mo.pdkCardHelper.sortCard(nums);
            var cards = [];
            for (var j = 0; j < nums.length; j++){
                var card = this.addCard();
                card.setCardNum(nums[j]);
                cards.push(card);
            }

            this.mPlayers[i].setInhandCard(cards);
        }
    },

    //打出去的牌和操作
    setChupai : function(dealCardNumss, LastDealSeat) {
        for (var i = 0; i < 3; i++){
            var nums = dealCardNumss[i];

            //牌不够了获取一个
            if (nums != null){
                var left = nums.length - this.mPlayers[i].dealCards.length;
                if (left > 0){
                    for (var j = 0; j < left; j++){
                        var card = this.addCard();
                        this.mPlayers[i].dealCards.push(card);
                    }
                }
            }

            this.mPlayers[i].setChupai(nums, LastDealSeat == i);
        }

        if (LastDealSeat == 0 || LastDealSeat == 1 || LastDealSeat == 2){
            var cardNums = dealCardNumss[LastDealSeat];
            if (cardNums != null && cardNums.length > 0){
                var cardType = mo.pdkCardRule.getCardType(cardNums);
                var pos = mo.pdkCardHelper.getDealCardCenterPos(LastDealSeat, cardNums.length);
                mo.pdkTable.mPdkPlayEffectLayer.playPdkEffect(cardType, pos);
            }
        }
    },

    //玩家打出手牌
    setDealCardNums : function(dealCardNums, dealSeat){
        mo.pdkCardHelper.sortCard(dealCardNums);
        if (dealSeat == 0) {
            for (var i = 0; i < dealCardNums.length; i++){
                for (var j = 0; j < this.mPlayers[dealSeat].inhandCards.length; j++){
                    var card = this.mPlayers[dealSeat].inhandCards[j];
                    if (dealCardNums[i] == card.mCardNum){
                        card.setVisible(false);
                        this.mPlayers[dealSeat].hideCards.push(card);

                        this.mPlayers[dealSeat].inhandCards.splice(j, 1);
                        j--;
                    }
                }
            }

            this.mPlayers[dealSeat].updateInhand();
        }
    },

    //结算 翻开玩家手牌
    openInhandCards : function(handCardNumss){
        //清除自己的手牌
        for (var i = 0; i < this.mPlayers[0].inhandCards.length; i++){
            this.mPlayers[0].inhandCards[i].setVisible(false);
        }

        //翻开手牌
        for (var i = 0; i < 3; i++){
            var nums = handCardNumss[i];

            //牌不够了获取一个
            if (nums != null){
                mo.pdkCardHelper.sortCard(nums);
                var left = nums.length - this.mPlayers[i].dealCards.length;
                if (left > 0){
                    for (var j = 0; j < left; j++){
                        var card = this.addCard();
                        this.mPlayers[i].dealCards.push(card);
                    }
                }
            }

            //赢的玩家翻开手牌为空
            this.mPlayers[i].setChupai(nums);
            // if (nums.length != 0){
            // this.mPlayers[i].setChupai(nums);
            // }
        }
    },

    //播放保单动画
    playBaodanAnim : function(seat){
        var pos = cc.p(mo.pdkTable.mPdkTableUILayer.mPlayerPoss[seat].x, mo.pdkTable.mPdkTableUILayer.mPlayerPoss[seat].y);
        pos.y -= 40;
        mo.pdkTable.mPdkPlayEffectLayer.playBaodan(pos);
    },

    //播放赢的动画
    playWinAnim : function(seat){
        var pos = cc.p(mo.pdkTable.mPdkTableUILayer.mPlayerPoss[seat].x, mo.pdkTable.mPdkTableUILayer.mPlayerPoss[seat].y);
        pos.y -= 40;
        if (seat == 0){
            pos.x = mo.gameSize.x / 2;
            pos.y = 100;
        }

        mo.pdkTable.mPdkPlayEffectLayer.playWin(pos);
    }
});